/****************************************************************************************
 Copyright © 2003-2012 ZTEsoft Corporation. All rights reserved. Reproduction or       <br>
 transmission in whole or in part, in any form or by any means, electronic, mechanical <br>
 or otherwise, is prohibited without the prior written consent of the copyright owner. <br>
 ****************************************************************************************/
package com.iwhalecloud.bss.kite.cucc.order.accept.chain.impl;

import com.iwhalecloud.bss.kite.client.api.shoppingcart.query.IQueryShoppingCartService;
import com.iwhalecloud.bss.kite.client.dto.shoppingcart.ShoppingCartGroupInstDTO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.order.accept.chain.AbstractOrderHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.order.client.dto.GroupInstContainer;
import com.iwhalecloud.bss.kite.order.client.dto.OrderRequest;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * <Description> <br>
 * 订单受理流程：数据准备
 *
 * @author xiao.xuzhang<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2020年2月12日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.order.accept.chain.impl <br>
 */
@Component
public class DataPreparationHandler extends AbstractOrderHandler {

    @Override
    public void doHandle(OrderRequest orderRequest) {
        // 场景对象
        this.initSceneInst(orderRequest);

        // 订单请求对象补充
        this.supplementOrderRequest(orderRequest);
    }

    /**
     * 加载场景对象
     *
     * @param orderRequest
     */
    private void initSceneInst(OrderRequest orderRequest) {
        IQueryShoppingCartService queryShoppingCartService = SpringUtil.getBean(IQueryShoppingCartService.class);
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        List<String> groupIds = orderRequest.getSceneInstIds();
        String custId = orderRequest.getCustId();
        GroupInstContainer groupInstContainer = new GroupInstContainer();
        List<ShoppingCartGroupInstDTO> shoppingCartGroupInsts = new ArrayList<>();
        Map<String, Map<String, Object>> keyValueMap = new HashMap<>();

        for (String groupId : groupIds) {
            ShoppingCartGroupInstDTO groupInst = queryShoppingCartService.getGroupInstDTOByGroupId(groupId, custId)
                .getResultObject();
            shoppingCartGroupInsts.add(groupInst);
            if (KeyConsts.ACCEP_MODE_O.equals(orderRequest.getAcceptMode())) {
                Map<String, Object> keyValue = querySceneInstService.getExtParameter(groupId);
                keyValueMap.put(groupId, keyValue);
            }
        }

        groupInstContainer.setKeyValueMap(keyValueMap);
        groupInstContainer.setShoppingCartGroupInsts(shoppingCartGroupInsts);
        orderRequest.setAcceptDate(LocalDateTime.now());
        orderRequest.setGroupInstContainer(groupInstContainer);
    }

    /**
     * 订单请求数据补充<br>
     * OrderRequest初始化逻辑：SceneInstMergeBO.initOrderRequest
     *
     * @param orderRequest <br>
     */
    private void supplementOrderRequest(OrderRequest orderRequest) {
        // 获取主场景实例对象
        ShoppingCartGroupInstDTO groupInst = this.getGroupInstByCustOrderId(orderRequest,
            orderRequest.getCustOrderId());

        // 设置受理来源
        if (KiteStringUtils.isEmpty(orderRequest.getAskSource())) {
            orderRequest.setAskSource(Optional.ofNullable(groupInst).map(m -> m.getOrderSource())
                .orElse(KeyConsts.ORDER_SOURCE_EVT_C_0001_1401));
        }
        // 设置受理模式
        if (KiteStringUtils.isEmpty(orderRequest.getAcceptMode())) {
            orderRequest.setAcceptMode(
                Optional.ofNullable(groupInst).map(m -> m.getAcceptMode()).orElse(KeyConsts.ACCEPT_MODE_A));
        }
    }

    private ShoppingCartGroupInstDTO getGroupInstByCustOrderId(OrderRequest orderRequest, String custOrderId) {
        return orderRequest.getGroupInstContainer().getShoppingCartGroupInsts().stream()
            .filter(p -> KiteStringUtils.isEqual(p.getCustOrderId(), custOrderId)).findFirst().orElse(null);
    }

}
